package nacos

import (
	"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
	"github.com/nacos-group/nacos-sdk-go/v2/vo"
	"github.com/spf13/viper"
	"log"
)

func initNacos() vo.NacosClientParam {
	if viper.GetBool("debug") {
		log.Printf("viper读取naocs连接 :::host: %s,port: %d,namespace: %s,timeout: %d,log-dir: %s,cache-dir: %s,log-level: %s", viper.GetString("host"), viper.GetUint64("port"), viper.GetString("namespace"), viper.GetUint64("timeout-ms"), viper.GetString("log-dir"), viper.GetString("cache-dir"), viper.GetString("log-level"))
	}

	//create ServerConfig
	sc := []constant.ServerConfig{
		*constant.NewServerConfig(viper.GetString("host"), viper.GetUint64("port"),
			constant.WithContextPath(viper.GetString("context-path"))),
	}

	//create ClientConfig
	cc := *constant.NewClientConfig(
		constant.WithNamespaceId(viper.GetString("namespace")),
		constant.WithTimeoutMs(viper.GetUint64("timeout-ms")),
		constant.WithNotLoadCacheAtStart(true),
		constant.WithLogDir(viper.GetString("log-dir")),
		constant.WithCacheDir(viper.GetString("cache-dir")),
		constant.WithLogLevel(viper.GetString("log-level")),
	)
	return vo.NacosClientParam{
		ClientConfig:  &cc,
		ServerConfigs: sc,
	}
}
